/*
线性表实现一元多项式操作
*/

#include <stdio.h>
#include <stdlib.h>

#define Status int
#define OK 1
#define ERROR 0

typedef struct node
{
    float *coef; //系数数组
    int maxSize; //最大容量
    int order; //最高阶数
}Polynomial;

void Add(Polynomial & A,Polynomial & B, Polynomial & C);
void ListPrint(Polynomial &list);

int main()
{
    float test[] = {1, 2, 3, 5};
    float *p = new float[4];
    for(int i = 0; i < 4; i++)
        *(p+i) = test[i];

    float test2[] = {3, 4, 5, 7};
    float *p2 = new float[4];
    for(int i = 0; i < 4; i++)
        *(p2+i) = test2[i];

    Polynomial A = {p, 10, 3};
    Polynomial B = {p2, 10, 3};
    Polynomial C;
    float *pc = new float[4];
    C.coef = pc;

    ListPrint(A);
    printf("\n");

    ListPrint(B);
    printf("\n");

    Add(A, B, C);

    ListPrint(C);
    printf("\n");
    return 0;
}

void Add(Polynomial & A,Polynomial & B, Polynomial & C)
{
    int i;
    int m = A.order;
    int n = B.order;
    for(i = 0; i <= m && i <= n; i++)
    {
        C.coef[i] = A.coef[i] + B.coef[i];
    }
    while(i <= m)
        C.coef[i] = A.coef[i];
    while(i <= n)
        C.coef[i] = B.coef[i];
    C.order = (m > n) ? m : n;
}

void ListPrint(Polynomial &list)
{
  for(int i = 0; i <= list.order; i++)
  {
      if(i == 0)
        printf("%.0f", list.coef[i]);
      else{
          printf("+%.0fx^%d", list.coef[i], i);
      }
  }
  printf("\n");
}
